Write a JavaScript program which receives an array of object of
students and retrun only name of students in an array who scored marks
above 49.
let student = {name : "John", marks : 50}, {name : "Alice", marks :
39},
{name : "Harish", marks : 87}, {name : "Liza", marks : 49} ]
//Output : ["John", "Harish"];
let student = [
{ name: "John", marks: 50 },
{name: "Alice", marks:39},
{ name: "Harish", marks: 87 },
{ name: "Liza", marks: 49 }
];
const myFunc1 = arr => arr.filter(e => e.marks > 49).map(e => e.name);
document.getElementById("ex1").innerText = myFunc1(student);
Write a JavaScript function to which get random string, filter out
duplicate characters/num and return unique characters/num in an array.
let filterStr("aabbccdef11255") //Output : ['a','b','c','d','e','f',1,2,5];
const myFunc2 = str => [...new Set(str)];
document.getElementById("ex2").innerText = myFunc2("aabbccdef11255");
Write a JavaScript program which receives 2 arguments. An array of
characters and array of numbers. Return an array of characters which
matches the index in second array.
let newArr( ['a', 'b', 'c', 'd','e'],[1,3]) //Output : ["b","d"];
const myFunc3 = (arr1,arr2) => {
let tempArr=[];
for (let x of arr2){
tempArr.push(arr1[x])
}
return tempArr;
}
document.getElementById("ex3").innerText = myFunc3(['a', 'b', 'c', 'd','e'],[1,3]);
Write a JavaScript function that hides email addresses to prevent
unauthorized access. (Display first 4 characters then apply before @
symbol)
let protectEmaiI("dcoders_Iab@exampIe.com") // dcod...@example.com
const myFunc4 = str => str.replace(str.split("@")[0].slice(4),"...");
document.getElementById("ex4").innerText = myFunc4("dcoders_Iab@exampIe.com");
Write a JavaScript function to calculate the sum of values in an array.
let arrSum([1,30,-20,0,50]);
Output : 61
const myFunc5 = arr => arr.filter(e => typeof e == "number").reduce((sum, e) => sum + e, 0);
document.getElementById("ex5").innerText = myFunc5([15, 35, -5, "a", 0, -15]);
Write a JavaScript function to round up an integer value to the next
multiple of 5.
let integerRound5(32);
let integerRound5(137);
const myFunc6 = num => Math.ceil(num / 5) * 5;
document.getElementById("ex6").innerText = myFunc6(32);
Write a JavaScript function to which receives an array of object and
index number as 2nd parameter. Remove the element from an array
based on index number provided and return final array.
let newArr([{x:1},{y:2},{z:3}], 1 ) //Output : [{x:1},{z:3}]
let newArr( [{a:10}, {b:20},{c:30}],5) //Output : [{a:10}, {b:20},{c:30}]
const myFunc7 = (arr, i) => arr.toSpliced(i, 1);
document.getElementById("ex7").innerText = JSON.stringify(myFunc7([{ x: 1 }, { y: 2 }, { z: 3 }], 1));
Write a JavaScript function to decapitalize first letter of string and
capitalise every other letters.
let Decapitalize("dcoders")// Output : dCODERS
let Decapitalize("javascript")// Output : jAVASCRIPT
const myFunc8 = str => {
var [first, ...rest] = str;
return first.toLowerCase() + rest.join("").toUpperCase();
}
document.getElementById("ex8").innerText = myFunc8("dcoders");
Write a JavaScript program to calculate how many numbers in the given
array are less than or equal to the given value.
let countNum( [1, 2, 3, 4, 5, 6, 7, 8, 9, 10], 5] ) //Output : 5
let countNum( [10, 15, 25, 35, 40, 55], 30 )
//Output : 3
const myFunc9 = (arr, value) => arr.filter(e => e <= value).length;
document.getElementById("ex9").innerText = myFunc9([1, 2, 3, 4, 5, 6, 7, 8, 9, 10], 5);
Write a JavaScript function to sort (ascending order) the given array of
objects based on pricing of products.
let sortProducts({product: "phone", price:15000}, {product: "laptop",
price:75000}, {product: "camera", price:12000} l)
// Output : ({product: "camera", price:12000} , {product: "phone",
price:15000}, {product: "laptop", price:75000})l
const myFunc10 = arr => arr.sort((a,b) => a.price - b.price)
document.getElementById("ex10").innerText =JSON.stringify( myFunc10([{product: "phone", price:15000}, {product: "laptop",
price:75000}, {product: "camera", price:12000}]));